var loaded_datagrid = 0;
$(function() {
    var per_page = fromCookie('refs', 'per_page');
    if (per_page < 10) per_page = 100;
    $('#refs').datagrid({		
        pagination: true,
        pageSize: per_page,
        pageList: ['10', '25', '50', '100'],
        dataType: 'json',
        idField: 'i',
        columns: [[{
            field: 'n',
            title: getTranslation('Name'),
            width: 240,
            sortable: true
        },
        {
            field: 'e',
            title: getTranslation('Email'),
            width: 200,
            sortable: true
        },
        {
            field: 'm',
            title: getTranslation('Mobile'),
            width: 100,
            sortable: true
        },
        {
            field: 'p',
            title: getTranslation('Parent'),
            width: 200,
            sortable: true
        },
        {
            field: 'a',
            title: getTranslation('Amount'),
            width: 200,
            sortable: false,
            formatter: function(val, row, index) {
                return '';
            }
        },
        {
            field: 's',
            title: getTranslation('Status'),
            width: 80,
            sortable: true,
            formatter: function(val, row, index) {
                if (val == 1) return getTranslation('Active');
                else if (val == 3) return 'Inactive';
                else if (val == 2) return getTranslation('Blocked');
                else return getTranslation('New');
            }
        },
        {
            field: 'd',
            title: getTranslation('Creation date'),
            width: 80,
            sortable: true
        }]],
        fitColumns: true,
        fit: true,
        toolbar: '#ref_tb',
        onBeforeLoad: function(params) {
            toCookie('refs', 'per_page', params.rows);
        },
        onLoadSuccess: function(data) {
            if (typeof(data['rows']) != 'undefined' && typeof(data['rows'][0]) != 'undefined') {
                if (typeof(data['rows'][0]['amounts']) != 'undefined') {                   
                    $('#refs').datagrid('hideColumn', 'e');
                    $('#refs').datagrid('hideColumn', 'm');
                } else {
                    initRefTree();
                    $('#refs').datagrid('hideColumn', 'a');
                }
                if (typeof(data['rows'][0]['p']) != 'undefined') {
                    $('#refs').datagrid('showColumn', 'p');
                } else {
                    $('#refs').datagrid('hideColumn', 'p');
                }
            } else {
                $('#refs').datagrid('hideColumn', 'e');
                $('#refs').datagrid('hideColumn', 'm');
                $('#refs').datagrid('hideColumn', 'p');
                $('#refs').datagrid('hideColumn', 'a');
            }
        },
        rowStyler: function(index, row) {
            var style = '';
            if (row.color == 1) style += 'background-color:#efefef;';
            if (row.s == 3) style += 'color:#DDDDDD';
            if (row.s == 2) style += 'color:grey';
            return style;
        }
    })
    initRefLink();
    initRefs();
});
var lst_currencies = new Array();
var amounts = new Array();
function initRefs() {
    var per_page = fromCookie('refs', 'per_page');
    if (per_page < 10) per_page = 100;
    getData(1,per_page);
    var p = $('#refs').datagrid('getPager');
    if (p){
           $(p).pagination({
//              onBeforeRefresh:function(){
//              },
           onSelectPage : function(pageNumber, pageSize){
        	   getData(pageNumber,pageSize);
           }
           });
       }
}
var getData = function(pageNumber,pageSize){
	loadingShow();
	var rows= []; 
	   $.ajax({
			url:baselocation+"/referals/json/",
			type: 'POST',
			data: {
                'pageNumber': pageNumber,
                'pageSize' : pageSize
            },
			dataType: 'json',
			success:function(result){
				loadingHide();
				rows=result;
				$('#refs').datagrid("loadData",rows);
			}	
		})
}
function initRefLink() {
    $('#ref_link').linkbutton();
}
function showRefLink() {
	var link=$("#email").val();
    var form = '';
    form += '<div id="ref_link_dialog">';
    form += '<div style="padding:5px;"><input type="text" readonly="readonly" value="https://mmmplus.cc/?i='+link+'" style="width:99%;" /></div>';
    form += '</div>';
    form += '<div id="ref_link_dialog_buttons" iconCls="icon-info">';
    form += '<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeRefLink(); return false;">' + getTranslation('Close') + '</a>';
    form += '</div>';
    $(form).appendTo('body');
    $("#ref_link_dialog_buttons a").linkbutton();
    $('#ref_link_dialog').dialog({
        width: 700,
        height: 105,
        title: getTranslation('referals_my_referal_link'),
        modal: true,
        buttons: '#ref_link_dialog_buttons',
        onClose: closeRefLink,
        onOpen: function() {
           
        }
    });
}
function closeRefLink() {
    $('#ref_link_dialog').dialog('destroy');
}